package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.PipeBoNumTjController;
import com.antoco.operation.bigscreen.model.dto.PipeBoNumTjDTO;
import com.antoco.operation.bigscreen.model.entity.PipeBoNumTj;
import com.antoco.operation.bigscreen.second.ValidList;
import com.antoco.operation.bigscreen.mapper.PipeBoNumTjMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.PipeBoNumTjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/pipe-bo-num-tj")
@RestController
public class PipeBoNumTjControllerImpl implements PipeBoNumTjController {

    @Autowired
    private PipeBoNumTjService pipeBoNumTjService;

    @Autowired
    private PipeBoNumTjMapper pipeBoNumTjMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public PipeBoNumTjDTO save(@RequestBody PipeBoNumTjDTO pipeBoNumTjDTO) {
        PipeBoNumTj pipeBoNumTj = pipeBoNumTjMapper.asEntity(pipeBoNumTjDTO);
        pipeBoNumTjService.save(pipeBoNumTj);
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeBoNumTjDTO;
    }

    @Override
    @PostMapping("/save-lists")
    @ResponseStatus(HttpStatus.CREATED)
    public List<PipeBoNumTjDTO> saveLists(@Validated @RequestBody ValidList<PipeBoNumTjDTO> pipeBoNumTjDTOLists) {
        pipeBoNumTjService.save(pipeBoNumTjMapper.asEntityList(pipeBoNumTjDTOLists));
        //当完成数据的操作后，调用该封装的方法获取变化后的数据进行推送
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeBoNumTjDTOLists;
    }

    @Override
    @GetMapping("/{id}")
    public PipeBoNumTjDTO findById(@PathVariable("id") String id) {
        PipeBoNumTj pipeBoNumTj = pipeBoNumTjService.findById(id).orElse(null);
        return pipeBoNumTjMapper.asDTO(pipeBoNumTj);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        pipeBoNumTjService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<PipeBoNumTjDTO> list() {
        return pipeBoNumTjMapper.asDTOList(pipeBoNumTjService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<PipeBoNumTjDTO> pageQuery(Pageable pageable) {
        Page<PipeBoNumTj> pipeBoNumTjPage = pipeBoNumTjService.findAll(pageable);
        List<PipeBoNumTjDTO> dtoList = pipeBoNumTjPage
                .stream()
                .map(pipeBoNumTjMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, pipeBoNumTjPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public PipeBoNumTjDTO update(@RequestBody PipeBoNumTjDTO pipeBoNumTjDTO, @PathVariable("id") String id) {
        PipeBoNumTj pipeBoNumTj = pipeBoNumTjMapper.asEntity(pipeBoNumTjDTO);
        pipeBoNumTjMapper.asDTO(pipeBoNumTjService.update(pipeBoNumTj, id));
        channelDataService.pushAllLists(this.getClass().getName());
        return pipeBoNumTjDTO;
    }
}